import java.util.*;

/**
 * @author LKQ
 * @date 2022/2/23 22:43
 * @description
 */
public class Solution2 {
    public static void main(String[] args) {
        Solution2 solution = new Solution2();
        int[] arr1 = {28,18,22,8,44,17}, arr2 = {22,28,8,18};
        solution.relativeSortArray(arr1, arr2);
    }
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        // 存储排序的值
        List<Integer> list = new ArrayList<>();
        for (int arr: arr1) {
            list.add(arr);
        }
        // 存储值的位置
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i< arr2.length; i++) {
            map.put(arr2[i], i);
        }
        Collections.sort(list, (x , y)-> {
            if (map.containsKey(x) || map.containsKey(y)) {
                return map.getOrDefault(x, 1001) - map.getOrDefault(y, 1001);
            }
            return x - y;
        });
        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = list.get(i);
        }
        return arr1;
    }
}
